
// 获取全部动态路由组件地址
let modules = import.meta.glob('../views/**/*.vue')

// 根级菜单
const rootRouter = {
    name: 'Home',
    path: '/',
    component: () => import('../views/Home.vue'),
    meta: {
        title: '后台管理'
    },
    redirect: '/index',
    children: [
        {
          path: '/index',
          name: 'Index',
          meta: {
              title: "首页"
          },
          component: () => import('../views/sys/Index.vue')
        },
        {
          path: '/userCenter',
          name: 'UserCenter',
          meta: {
              title: "用户中心"
          },
          component: () => import('../views/sys/UserCenter.vue')
        },
        {
            path: '/student/list',
            name: 'StudentList',
            component: () => import('../views/student/list.vue')
        }
    ]
  }

/**
 * 异步生成路由
 * @param {*} menuList 
 * @returns 
 */
export const renderAsyncRouter = (menuList) => {
    return new Promise(reslove => {
        // const treeList = menuList
        const addRouter = menuList
        // 将列表转换成树形结构
        // treeMap(treeList, addRouter, null)

        // 生成路由表
        const renderRouter = generator(addRouter)
        // console.log(renderRouter)

        const asyncRouter = []
        // rootRouter.children = renderRouter.concat(hideRouter)

        asyncRouter.push(
            {
                hide: true,
                name: '404',
                path: '/:pathMatch(.*)*',
                component: () => import('../views/Exception/404.vue')
            },
            rootRouter,
        )

        // console.log(asyncRouter)

        reslove({ asyncRouter, renderRouter })
    })

}

/**
 * 生成路由表
 * @param {*} routerMap 
 * @param {*} parent 
 * @returns 
 */
export const generator = (routerMap, parent) => {
    const routes = routerMap.map(item => {
        const currentRouter = {
            // 如果路由设置了 path，则作为默认 path，否则 路由地址 动态拼接生成如 /dashboard/workplace
            path: item.path,
            // 路由名称，建议唯一
            name: item.name,
            // 该路由对应页面的 组件 :方案1
            // component: constantRouterComponents[item.component],
            // 该路由对应页面的 组件 :方案2 (动态加载)
            // component: () => import(`../views/${item.component}.vue`),
            component: modules[`../views/${item.component}.vue`],
            // component: (constantRouterComponents[item.component || item.key]) || (() => import(`@/views/${item.component}`)),
            // meta: 页面标题, 菜单图标, 页面权限(供指令权限用，可去掉)
            meta: {
                title: item.title,
                icon: item.icon || undefined,
                del: !item.component
            }
        }
        
        item.redirect && (currentRouter.redirect = item.redirect)
        // 是否有子菜单，并递归处理
        if (item.children && item.children.length > 0) {
            // Recursion
            currentRouter.children = generator(item.children, currentRouter)
        }
        return currentRouter
    })

    return routes
}

/**
 * 列表转树形结构
 * @param {*} menuTree 
 * @param {*} tree 
 * @param {*} partentId 
 */
const treeMap = (menuTree, tree, partentId) => {

    menuTree.forEach(item => {
        if (item.parentId == partentId) {
            const child = {
                name: item.name,
                title: item.title,
                children: [],
                // redirect: item.redirectUrl,
                component: item.component,
                path: item.path,
                icon: item.icon
            }

            treeMap(menuTree, child.children, item._id)
            if (child.children.length == 0) {
                delete child.children
            }

            tree.push(child)
        }
    })
}


// 菜单转换成路由
const menu2Route = (menu) => {
    // 有组件才转换
    if(!menu.component) {
      return null
    }
  
    let route = {
      name: menu.name,
      path: menu.path,
      meta: {
        icon: menu.icon,
        title: menu.title
      },
      component: () => import(`../views/${menu.component}.vue`)
    }
    return route
}